package com.zkyc.framework.schedulejob.task;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.zkyc.framework.common.mapper.WebClickRateMapper;
import com.zkyc.framework.common.pojo.WebClickRate;
import com.zkyc.framework.common.utils.DateUtil;
import com.zkyc.framework.schedulejob.config.JedisPoolFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import static com.zkyc.framework.common.constant.Constant.*;

/**
 * @description 定时更新访客量
 */
@Slf4j
@Component
public class ViewsTask {
    @Autowired
    private WebClickRateMapper webClickRateMapper;

    /**
     * 每四小时同步一次
     */
    @Scheduled(cron = "0 0 0/4 * * ?")
    //@Scheduled(cron = "*/5 * * * * ?")
    public void process() {
        List<Integer> webSiteIds = webClickRateMapper.getAllWebSiteId();
        String currentDate = DateUtil.getCurrentTimeStr();
        for (Integer webSiteId : webSiteIds) {
            String clickRate = JedisPoolFactory.select(REDIS_PREFIX + VIEWS + webSiteId + StringPool.UNDERSCORE + currentDate);
            if (clickRate == null) {
                clickRate = StringPool.ZERO;
            }
            QueryWrapper<WebClickRate> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("website_id", webSiteId);
            Date date = new Date();
            queryWrapper.apply("date_format(click_date,'%Y-%m-%d') =" + "date_format({0},'%Y-%m-%d')", date);
            WebClickRate webClickRate = webClickRateMapper.selectOne(queryWrapper);
            if (webClickRate == null || !DateUtils.isSameDay(new Date(), webClickRate.getClickDate())) {
                webClickRate = new WebClickRate();
                webClickRate.setWebsiteId(webSiteId);
                webClickRate.setClickRate(Integer.parseInt(clickRate));
                System.out.println("执行的是插入：websiteId: " + webSiteId + ",现在的日期是:" + date + ",currentDate是:" + currentDate + ",点击量是:" + clickRate + ",webClickDate:" + webClickRate.getClickDate());
                webClickRateMapper.insert(webClickRate);
            } else {
                webClickRate.setClickRate(Integer.parseInt(clickRate));
                System.out.println("执行的是更新：websiteId: " + webSiteId + ",现在的日期是:" + date + ",currentDate是:" + currentDate + ",点击量是:" + clickRate + ",webClickDate:" + webClickRate.getClickDate());
                webClickRateMapper.updateById(webClickRate);
            }
        }
        System.out.println("----------------------");
        System.out.println("----------------------");
        System.out.println("----------------------");
        System.out.println("----------------------");
        System.out.println("----------------------");
        System.out.println("ViewsTask执行了");
        System.out.println("----------------------");
        System.out.println("----------------------");
        System.out.println("----------------------");
        System.out.println("----------------------");
        System.out.println("----------------------");
    }

    /**
     * 每天执行一次
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void run() {
        // 获取当前日期前一天的日期
        LocalDate localDate = LocalDate.now().minusDays(1);
        List<Integer> webSiteIds = webClickRateMapper.getAllWebSiteId();
        for (Integer webSiteId : webSiteIds) {
            JedisPoolFactory.delete(REDIS_PREFIX + VIEWS + webSiteId + StringPool.UNDERSCORE + localDate);
            System.out.println("删除了" + localDate + "的访问量");
        }
    }
}
